package demo4.controller;

import demo4.iml.IUserMapper;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import java.util.Scanner;

@Controller
public class UserController {
    @Autowired
    private IUserMapper userMapper;
    Scanner scanner = new Scanner(System.in);

    public void start(){
        System.out.println("用户系统");
        System.out.println("1:用户注册");
        System.out.println("2:用户登录");
        System.out.println("3:用户查询");
        System.out.println("4:用户修改");
        System.out.println("5:用户删除");
        System.out.println("6:用户退出");
        int num=scanner.nextInt();
        switch (num) {
            case 1:
                System.out.println("用户注册");
                userReg();
                break;
            case 2:
                System.out.println("用户登录");
                userLogin();
                break;
            case 3:
                System.out.println("用户查询");
                break;
            case 4:
                System.out.println("用户修改");
                break;
            case 5:
                System.out.println("用户删除");
                break;
            case 6:
                System.out.println("用户退出");
                break;
            default:
                System.out.println("输入错误");
                break;
        }
    }

    public void userList(){
        System.out.println("用户列表");
        for (User user : userMapper.selectAll()) {
            System.out.println(user);
        }
    }
    private void userLogin(){
        System.out.println("请输入用户名");
        String username=scanner.next();
        System.out.println("请输入密码");
        String password=scanner.next();
        User user = userMapper.selectByUsername(username);
        if(user!=null){
            if(user.getPassword().equals(password)){
                System.out.println(username+"登录成功");
            }else {
                System.out.println("密码错误");
            }
        }else {
            System.out.println("用户不存在");
        }
    }

    private void userReg(){
        System.out.println("请输入用户名：");
        String name = scanner.next();
        System.out.println("请输入密码：");
        String password = scanner.next();
        System.out.println("请输入昵称：");
        String nickName = scanner.next();
        System.out.println("请输入年龄：");
        int age = scanner.nextInt();

        User user = new User(name,password,nickName,age);
        int num=userMapper.insert(user);
        System.out.println(num>0?"注册成功":"注册失败");
        System.out.println("注册成功");
    }
}
